part of '../../repository.dart';

const _kAppStorePurchasedSubscriptionTransactionOriginalTransactionIdKey =
    'original_transaction_id';
const _kAppStorePurchasedSubscriptionTransactionExpirationTimeKey = 'expiration_time';

class _AppStorePurchasedSubscriptionTransactionDto extends Equatable
    implements _AppStoreTransactionDto {
  const _AppStorePurchasedSubscriptionTransactionDto._internal({
    required this.id,
    required this.timeDto,
    required this.offerTypeDto,
    required this.ownershipTypeDto,
    required this.revocationInfoDto,
    required this.originalTransactionId,
    required this.expirationTimeDto,
  });

  @override
  final String id;

  @override
  final ApiTimeDto timeDto;

  @override
  final _AppStoreTransactionOfferTypeDto offerTypeDto;

  @override
  final _AppStoreTransactionOwnershipTypeDto ownershipTypeDto;

  @override
  final _AppStoreTransactionRevocationInfoDto? revocationInfoDto;

  final String originalTransactionId;
  final ApiTimeDto expirationTimeDto;

  @override
  List<Object?> get props {
    return [
      id,
      timeDto,
      originalTransactionId,
      offerTypeDto,
      ownershipTypeDto,
      revocationInfoDto,
      expirationTimeDto,
    ];
  }

  factory _AppStorePurchasedSubscriptionTransactionDto.fromJson(Map<String, dynamic> json) {
    final timeString = json[_kAppStoreTransactionTimeKey] as String;
    final timeDto = ApiTimeDto.fromString(timeString);

    final offerTypeString = json[_kAppStoreTransactionOfferTypeKey] as String;
    final offerTypeDto = _AppStoreTransactionOfferTypeDto.fromString(offerTypeString);

    final ownershipTypeString = json[_kAppStoreTransactionOwnershipTypeKey] as String;
    final ownershipTypeDto = _AppStoreTransactionOwnershipTypeDto.fromString(ownershipTypeString);

    final revocationInfoJson =
        json[_kAppStoreTransactionRevocationInfoKey] as Map<String, dynamic>?;

    _AppStoreTransactionRevocationInfoDto? revocationInfoDto;

    if (revocationInfoJson != null) {
      revocationInfoDto = _AppStoreTransactionRevocationInfoDto.fromJson(revocationInfoJson);
    }

    final expirationTimeString =
        json[_kAppStorePurchasedSubscriptionTransactionExpirationTimeKey] as String;
    final expirationTimeDto = ApiTimeDto.fromString(expirationTimeString);

    return _AppStorePurchasedSubscriptionTransactionDto._internal(
      id: json[_kAppStoreTransactionIdKey],
      timeDto: timeDto,
      originalTransactionId:
          json[_kAppStorePurchasedSubscriptionTransactionOriginalTransactionIdKey],
      offerTypeDto: offerTypeDto,
      ownershipTypeDto: ownershipTypeDto,
      revocationInfoDto: revocationInfoDto,
      expirationTimeDto: expirationTimeDto,
    );
  }

  @override
  AppStorePurchasedSubscriptionTransaction toEntity() {
    return AppStorePurchasedSubscriptionTransaction(
      id: id,
      time: timeDto.toEntity(),
      originalTransactionId: originalTransactionId,
      offerType: offerTypeDto.toEntity(),
      ownershipType: ownershipTypeDto.toEntity(),
      revocationInfo: revocationInfoDto?.toEntity(),
      expirationTime: expirationTimeDto.toEntity(),
    );
  }
}
